package org.yuanqiframework.yuanqi.boot.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.yuanqiframework.yuanqi.boot.auth.AuthService;
import org.yuanqiframework.yuanqi.web.utils.WebUploadFileUtils;

import javax.annotation.Resource;

@Slf4j
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    @Resource
    private AuthService authService;

    /**
     * 把图片请求映射到本地文件夹
     *
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        String imagesLocation = "file:" + WebUploadFileUtils.UPLOAD_PATH_JAR_HOME;
        registry.addResourceHandler("/" + WebUploadFileUtils.UPLOAD_PATH_DIR_NAME + "/**")
                .addResourceLocations(imagesLocation);
    }

    /**
     * 拦截器 注册
     *
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 鉴权拦截器
        authService.registerAuthInterceptor(registry);
    }
}
